package mamikeji.liaowa.receiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;

import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;
import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.PushMessage;
import mamikeji.liaowa.db.PushMessageManager;
import mamikeji.liaowa.ui.activity.FaceActivity;
import mamikeji.liaowa.utils.DateUtils;

import static android.content.Context.NOTIFICATION_SERVICE;

/**
 * Created by 廉冲-android开发 on 2017/6/16.
 */
public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            Bundle bundle = intent.getExtras();
            if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
                String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);

            } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
                JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
                String title = bundle.getString(JPushInterface.EXTRA_TITLE);
                String content = bundle.getString(JPushInterface.EXTRA_MESSAGE);
                int type = json.optInt("type");
                String url = json.optString("url");
                insertDB(title, content, type, url, "false");
                setNotification(context, title, content);

            } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
                int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);

            } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
                //Log.d(TAG, "[MyReceiver] 用户点击打开了通知");

            } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {

            } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
                boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
            } else {
            }
        } catch (Exception e) {

        }

    }

    private void insertDB(String title, String content, int type, String url, String isRead) {
        PushMessage pushMessage = new PushMessage();
        pushMessage.setTitle(title);
        pushMessage.setContent(content);
        pushMessage.setType(type);
        pushMessage.setUrl(url);
        pushMessage.setIsRead(isRead);
        pushMessage.setTime(DateUtils.getHourAndMinute());
        PushMessageManager.insert(pushMessage);
    }

    private void setNotification(Context context, String title, String content) {
        int notifyId = 199;
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
        mBuilder.setContentTitle(title)//设置通知栏标题
                .setContentText(content) //设置通知栏显示内容
                .setContentIntent(getDefalutIntent(context, Notification.FLAG_AUTO_CANCEL)) //设置通知栏点击意图
                //.setNumber(number) //设置通知集合的数量
                .setTicker("您有一条消息") //通知首次出现在通知栏，带上升动画效果的
                .setWhen(System.currentTimeMillis())//通知产生的时间，会在通知信息里显示，一般是系统获取到的时间
                .setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级
                .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消
                .setOngoing(false)//ture，设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
                .setDefaults(Notification.DEFAULT_ALL)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置，使用defaults属性，可以组合
                //Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission
                .setSmallIcon(R.drawable.ic_launcher);//设置通知小ICON
        mNotificationManager.notify(notifyId, mBuilder.build());
    }

    public PendingIntent getDefalutIntent(Context context, int flags) {
        Intent intent = new Intent();
        intent.setClass(context, FaceActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, flags);
        return pendingIntent;
    }
}
